//package com.qf.mybatisplus.function.dingding;
//
//import com.aliyun.dingtalktodo_1_0.models.CreateTodoTaskHeaders;
//import com.aliyun.dingtalktodo_1_0.models.CreateTodoTaskRequest;
//import com.aliyun.dingtalktodo_1_0.models.CreateTodoTaskResponse;
//import com.aliyun.tea.*;
//import com.aliyun.teaopenapi.models.Config;
//import com.aliyun.teautil.models.*;
//
//import com.dingtalk.api.DefaultDingTalkClient;
//import com.dingtalk.api.DingTalkClient;
//import com.dingtalk.api.request.OapiGettokenRequest;
//import com.dingtalk.api.request.OapiV2UserGetRequest;
//import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
//import com.dingtalk.api.response.OapiGettokenResponse;
//import com.dingtalk.api.response.OapiV2UserGetResponse;
//import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
//import com.taobao.api.ApiException;
//import java.util.Arrays;
//
//
//
///**
//* @Description  新版发送钉钉待办任务
//* @Param
//* @Author 12136
//* @Date 11:08 2021/12/6
//* @return
//**/
//public class Test {
//
//
//    private String APP_KEY = "ding14ci3uxgdisf1mt7";
//    private String APP_SECRET = "sr7mSCUwLfJGSSlKQpor11du-5qCnIEMEicV6-rnRKgWayo34aRrSOihzo35Grfi";
//    private Long AGENT_ID = 1388513699L;
//
//
//    /**
//     * 使用 Token 初始化账号Client
//     * @return Client
//     * @throws Exception
//     */
//    public static com.aliyun.dingtalktodo_1_0.Client createClient() throws Exception {
//        Config config = new Config();
//        config.protocol = "https";
//        config.regionId = "central";
//        return new com.aliyun.dingtalktodo_1_0.Client(config);
//    }
//
//    /**
//     * 获取钉钉token
//     * @param appKey AppKey
//     * @param appSecret AppSecret
//     * @return token字符串
//     */
//    public static String getToken(String appKey, String appSecret) {
//        //token两小时更新一次
//        DingTalkClient getTokenclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
//        OapiGettokenRequest request = new OapiGettokenRequest();
//        request.setAppkey(appKey);
//        request.setAppsecret(appSecret);
//        request.setHttpMethod("GET");
//        OapiGettokenResponse response = null;
//        try {
//            response = getTokenclient.execute(request);
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }
//        return response.getAccessToken();
//    }
//
//    /**
//     * 通过手机号获取用户的userId
//     * @param mobile 手机号
//     * @param appKey AppKey
//     * @param appSecret AppSecret
//     * @return userId
//     */
//    public static String getUserId (String mobile, String appKey, String appSecret) {
//        //获取用户urid
//        DingTalkClient getUridClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
//        OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
//        req.setMobile(mobile);
//        OapiV2UserGetbymobileResponse rsp = null;
//        try {
//            rsp = getUridClient.execute(req, getToken(appKey, appSecret));
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }
//        return rsp.getResult().getUserid();
//    }
//
//
//    /**
//     * 通过userid获取用户对象
//     * @param userId userid
//     * @param appKey AppKey
//     * @param appSecret AppSecret
//     * @return 用户对象
//     */
//    public static OapiV2UserGetResponse.UserGetResponse getUserInfo (String userId, String appKey, String appSecret) {
//        //获取用户urid
//        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
//        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
//        req.setUserid(userId);
//        req.setLanguage("zh_CN");
//        OapiV2UserGetResponse rsp = null;
//        try {
//            rsp = client.execute(req, getToken(appKey, appSecret));
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }
//        return rsp.getResult();
//    }
//
//    public static void sendWorkRecordByMobile (String mobile, String title, String context, Long date, String appKey, String appSecret) {
//        // 发送人unionId
//        String unionIdC = getUserInfo(getUserId("13213018164", appKey, appSecret), appKey, appSecret).getUnionid();
//        // 接收人unionId
//        String unionId = getUserInfo(getUserId(mobile, appKey, appSecret), appKey, appSecret).getUnionid();
//        com.aliyun.dingtalktodo_1_0.Client client = null;
//        try {
//            client = createClient();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        CreateTodoTaskHeaders createTodoTaskHeaders = new CreateTodoTaskHeaders();
//        createTodoTaskHeaders.xAcsDingtalkAccessToken = getToken(appKey, appSecret);
//        CreateTodoTaskRequest.CreateTodoTaskRequestNotifyConfigs notifyConfigs = new CreateTodoTaskRequest.CreateTodoTaskRequestNotifyConfigs()
//                .setDingNotify("1");
////        CreateTodoTaskRequest.CreateTodoTaskRequestDetailUrl detailUrl = new CreateTodoTaskRequest.CreateTodoTaskRequestDetailUrl()
////                // app内打开的链接
////                .setAppUrl("https://www.dingtalk.com")
////                // pc端打开的链接
////                .setPcUrl("https://www.dingtalk.com");
//        CreateTodoTaskRequest createTodoTaskRequest = new CreateTodoTaskRequest()
//                // 待办标题
//                .setSubject(title)
//                .setCreatorId(unionIdC)
//                // 待办内容
//                .setDescription(context)
//                // 截止时间，使用时间戳
//                .setDueTime(date)
//                // 接收消息的人
//                .setExecutorIds(Arrays.asList(
//                        unionId
//                ))
////                .setDetailUrl(detailUrl)
//                .setIsOnlyShowExecutor(true)
//                // 紧急程度，分4档，此处为2档普通
//                .setPriority(20)
//                .setSourceId("3")
//                .setNotifyConfigs(notifyConfigs);
//        try {
//            CreateTodoTaskResponse response = client.createTodoTaskWithOptions(unionId, createTodoTaskRequest, createTodoTaskHeaders, new RuntimeOptions());
//            System.err.println("完成状态:"+response.getBody().getDone());
//            System.err.println("完成时间:"+response.getBody().getFinishTime());
//                } catch (TeaException err) {
//                    if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
//                // err 中含有 code 和 message 属性，可帮助开发定位问题
//                System.out.println(err.message);
//            }
//
//        } catch (Exception _err) {
//            TeaException err = new TeaException(_err.getMessage(), _err);
//            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
//                // err 中含有 code 和 message 属性，可帮助开发定位问题
//                System.out.println(err.message);
//            }
//
//        }
//    }
//
//    public static void main(String[] args) {
//
//        String APP_KEY = "ding14ci3uxgdisf1mt7";
//        String APP_SECRET = "sr7mSCUwLfJGSSlKQpor11du-5qCnIEMEicV6-rnRKgWayo34aRrSOihzo35Grfi";
//        Long AGENT_ID = 1388513699L;
//        Test test = new Test();
//
//        // 截止时间戳
//        Long date = 1639041830557L;
//
//        Test.sendWorkRecordByMobile("13213018164", "自助机故障", "不动产登记中心：1001001自助机缺纸，请尽快处理", date,APP_KEY , APP_SECRET);
//
//    }
//
//}
